package com.uenpay.im.ui.old.activity.company;

import com.qiyunxin.android.http.net.HttpRequestType;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-22 13:55:24
 * @copyRight http://www.qiyunxin.com
 */
public class CompanyHandle {

	public interface ICompanyResultListener {
		void onResult(int status, String msg);
	}

	/**
	 * 添加公司列表
	 * 
	 * @param companyName
	 * @param mCompanyResultListener
	 */
	public void createCompany(int entid, String companyName,
			final ICompanyResultListener mCompanyResultListener) {

		AddCompanyInvokeItem item = new AddCompanyInvokeItem(entid, companyName);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							AddCompanyInvokeItem.AddCompanyInvokeItemResult result = ((AddCompanyInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mCompanyResultListener.onResult(result.status,
										result.msg);
							} else {
								mCompanyResultListener.onResult(-1, failReason);
							}
						} else {
							mCompanyResultListener.onResult(-1, failReason);
						}
					}
				});

	}

	public interface IGetMyCompanyListener {
		void onGetCompanyResult(int status, String msg,
												ArrayList<CompanyEntity> arrayList);
	}

	/**
	 * 获取我的公司列表
	 * 
	 * @param type
	 *            1:获取我的企业列表
	 * @param myexist
	 *            :0 当前用户不在的企业 myexist:1 当前用户所在的企业 myexist:空 所有的企业
	 */
	public void getMyCompanyList(int type, int myexist,
			final IGetMyCompanyListener mIGetMyCompanyListener) {
		CompanyListInvokeItem item = new CompanyListInvokeItem(type, myexist);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							CompanyListInvokeItem.CompanyListInvokeItemResult result = ((CompanyListInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIGetMyCompanyListener.onGetCompanyResult(
										result.status, result.msg,
										result.arrayList);
							} else {
								mIGetMyCompanyListener.onGetCompanyResult(-1,
										"", null);
							}
						} else {
							mIGetMyCompanyListener.onGetCompanyResult(-1,
									failReason, null);
						}
					}
				});

	}

	public interface IGetDepartListResult {
		void onGetDepartResult(int status, String msg,
											   ArrayList<DepartmentEntity> arrayList);
	}

	/**
	 * 获取子部门
	 * 
	 * @param pid
	 * @param entid
	 * @param mIGetDepartListResult
	 */
	public void getDepartByPidList(int pid, int entid,
			final IGetDepartListResult mIGetDepartListResult) {
		GetDepartByPidListInvokeItem item = new GetDepartByPidListInvokeItem(
				pid, entid);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							GetDepartByPidListInvokeItem.GetDepartByPidListInvokeItemResult result = ((GetDepartByPidListInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIGetDepartListResult.onGetDepartResult(
										result.status, result.msg,
										result.arrayList);
							} else {
								mIGetDepartListResult.onGetDepartResult(-1,
										failReason, null);
							}
						} else {
							mIGetDepartListResult.onGetDepartResult(-1,
									failReason, null);
						}
					}
				});
	}

	/**
	 * 删除公司
	 * 
	 * @param entid
	 * @param mICompanyResultListener
	 */
	public void deleteCompany(int entid,
			final ICompanyResultListener mICompanyResultListener) {
		DeleteCompanyInvokeItem item = new DeleteCompanyInvokeItem(entid);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							DeleteCompanyInvokeItem.DeleteCompanyInvokeItemResult result = ((DeleteCompanyInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mICompanyResultListener.onResult(result.status,
										result.msg);
							} else {
								mICompanyResultListener
										.onResult(-1, failReason);
							}
						} else {
							mICompanyResultListener.onResult(-1, failReason);
						}
					}
				});

	}

	/**
	 * 创建部门
	 * 
	 * @param entid
	 *            企业id
	 * @param pid
	 *            父部门id
	 * @param departname
	 *            部门id
	 * @param mICompanyResultListener
	 */
	public void createDepartment(int entid, int pid, String departname,
			final ICompanyResultListener mICompanyResultListener) {
		AddDepartmentsInvokeItem item = new AddDepartmentsInvokeItem(entid,
				pid, departname);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							AddDepartmentsInvokeItem.AddDepartmentsInvokeItemResult result = ((AddDepartmentsInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mICompanyResultListener.onResult(result.status,
										result.msg);
							} else {
								mICompanyResultListener
										.onResult(-1, failReason);
							}
						} else {
							mICompanyResultListener.onResult(-1, failReason);
						}
					}
				});
	}

	/**
	 * 删除部门
	 * 
	 * @param entid
	 * @param departid
	 * @param mICompanyResultListener
	 */
	public void deleteDepartment(int entid, int departid,
			final ICompanyResultListener mICompanyResultListener) {
		DeleteDepartmentsInvokeItem item = new DeleteDepartmentsInvokeItem(
				entid, departid);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							DeleteDepartmentsInvokeItem.DeleteDepartmentsInvokeItemResult result = ((DeleteDepartmentsInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mICompanyResultListener.onResult(result.status,
										result.msg);
							} else {
								mICompanyResultListener
										.onResult(-1, failReason);
							}
						} else {
							mICompanyResultListener.onResult(-1, failReason);
						}
					}
				});
	}

	/**
	 * 修改部门
	 * 
	 * @param departid
	 * @param entid
	 * @param pid
	 * @param departname
	 * @param mICompanyResultListener
	 */
	public void updateDepartment(int departid, int entid, int pid,
			String departname,
			final ICompanyResultListener mICompanyResultListener) {
		AddDepartmentsInvokeItem item = new AddDepartmentsInvokeItem(departid,
				entid, pid, departname);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							AddDepartmentsInvokeItem.AddDepartmentsInvokeItemResult result = ((AddDepartmentsInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mICompanyResultListener.onResult(result.status,
										result.msg);
							} else {
								mICompanyResultListener.onResult(-1, "");
							}
						} else {
							mICompanyResultListener.onResult(-1, failReason);
						}
					}
				});

	}

	/**
	 * 添加员工到部门
	 * 
	 * @param departid
	 *            部门id
	 * @param entid
	 *            企业id
	 * @param mobile
	 *            手机号
	 * @param nickname
	 *            名称
	 * @param position
	 *            职位
	 */
	public void addEmployee(int departid, int entid, String mobile,
			String nickname, String position,
			final ICompanyResultListener mICompanyResultListener) {
		AddEmployeeInvokeItem item = new AddEmployeeInvokeItem(departid, entid,
				mobile, nickname, position);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							AddEmployeeInvokeItem.AddEmployeeInvokeItemResult result = ((AddEmployeeInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mICompanyResultListener.onResult(result.status,
										result.msg);
							} else {
								mICompanyResultListener.onResult(-1, "");
							}
						} else {
							mICompanyResultListener.onResult(-1, failReason);
						}
					}
				});

	}

	/**
	 * 修改员工
	 * 
	 * @param custid
	 * @param departid
	 * @param entid
	 * @param position
	 * @param nickname
	 * @param mICompanyResultListener
	 */
	public void updateEmployee(String custid, int departid, int entid,
			String position, String nickname,
			final ICompanyResultListener mICompanyResultListener) {
		AddEmployeeInvokeItem item = new AddEmployeeInvokeItem(custid,
				departid, entid, position, nickname);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							AddEmployeeInvokeItem.AddEmployeeInvokeItemResult result = ((AddEmployeeInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mICompanyResultListener.onResult(result.status,
										result.msg);
							} else {
								mICompanyResultListener.onResult(-1, "");
							}
						} else {
							mICompanyResultListener.onResult(-1, failReason);
						}
					}
				});

	}

	public interface IGetCustByDepartListener {
		void onGetCustByDepartResult(int status, String msg,
													 ArrayList<EmployeeEntity> arrayList);
	}

	/**
	 * 获取部门下员工
	 * 
	 * @param departid
	 *            部门id
	 * @param entid
	 *            企业id
	 * @param mIGetCustByDepartListener
	 */
	public void getCustbyDepart(int departid, int entid,
			final IGetCustByDepartListener mIGetCustByDepartListener) {
		GetEntCustByDepartInvokeItem item = new GetEntCustByDepartInvokeItem(
				departid, entid);

		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							GetEntCustByDepartInvokeItem.GetEntCustByDepartInvokeItemResult result = ((GetEntCustByDepartInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIGetCustByDepartListener
										.onGetCustByDepartResult(result.status,
												result.msg, result.arrayList);
							} else {
								mIGetCustByDepartListener
										.onGetCustByDepartResult(-1,
												failReason, null);
							}
						} else {
							mIGetCustByDepartListener.onGetCustByDepartResult(
									-1, failReason, null);
						}
					}
				});

	}

	/**
	 * 将某员工将部门中移除
	 * 
	 * @param entid
	 * @param departid
	 * @param custids
	 * @param mICompanyResultListener
	 */
	public void deleteEmployee(int entid, int departid, String custids,
			final ICompanyResultListener mICompanyResultListener) {
		DeleteEmployeeInvokeItem item = new DeleteEmployeeInvokeItem(entid,
				departid, custids);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							DeleteEmployeeInvokeItem.DeleteEmployeeInvokeItemResult result = ((DeleteEmployeeInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mICompanyResultListener.onResult(result.status,
										result.msg);
							} else {
								mICompanyResultListener
										.onResult(-1, failReason);
							}
						} else {
							mICompanyResultListener.onResult(-1, failReason);
						}
					}
				});

	}

}
